﻿using SharpDX.DirectInput;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TableTool.Model;
using TlToolBase;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static TableTool.KeyboardHook;

namespace TableTool
{
    public partial class KeyDownClose : Form
    {
        /// <summary>
        /// 
        /// </summary>
        KeyboardHook keyboardHook { get; set; }
        /// <summary>
        /// 
        /// </summary>
        KeyDownCloseModel model { get; set; }
        private bool saveFalg1 { get; set; }
        private bool saveFalg2 { get; set; }
        private bool saveFalg3 { get; set; }
        public KeyDownClose()
        {
            InitializeComponent();
            keyboardHook = new KeyboardHook();
            this.Load += KeyDownClose_Load;
            this.FormClosing += KeyDownClose_FormClosing;
        }

        private void KeyDownClose_FormClosing(object? sender, FormClosingEventArgs e)
        {
            keyboardHook.KeyDownEvent -= hook_KeyDown;
            keyboardHook.Start();
            FileDataClass.Save(model);
        }

        private void KeyDownClose_Load(object? sender, EventArgs e)
        {
            model = FileDataClass.Get<KeyDownCloseModel>();
            keyboardHook.model = model;
            keyboardHook.KeyDownEvent += hook_KeyDown;
            keyboardHook.Start();
            SetLable();
        }
        private void hook_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void btnP1_Click(object sender, EventArgs e)
        {
            if (saveFalg1)
            {
                saveFalg1 = false;
            }
            else
            {
                if (model.P1 == null)
                {
                    model.P1 = new();
                }
                model.P1.Clear();
                SetLable();
                saveFalg1 = true;
            }
            btnP1.Text = saveFalg1 ? "停止记录" : "记录P1";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (saveFalg2)
            {
                saveFalg2 = false;
            }
            else
            {
                if (model.P2 == null)
                {
                    model.P2 = new();
                }
                model.P2.Clear();
                SetLable();
                saveFalg2 = true;
            }
            button2.Text = saveFalg2 ? "停止记录" : "记录P2";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (saveFalg3)
            {
                saveFalg3 = false;
            }
            else
            {
                if (model.P3 == null)
                {
                    model.P3 = new();
                }
                model.P3.Clear();
                SetLable();
                saveFalg3 = true;
            }
            button3.Text = saveFalg3 ? "停止记录" : "记录P3";
        }
        private void SetLable()
        {
            if (IsHandleCreated)
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new Action(SetLable));
                }
                else
                {
                    SetShowLable(label1, model.P1);
                    SetShowLable(label2, model.P2);
                    SetShowLable(label3, model.P3);
                }
            }
        }
        private void SetShowLable(Label label, List<Keys> keys)
        {
            label.Text = "";
            if (keys != null)
            {
                for (int i = 0; i < keys.Count; i++)
                {
                    if (i > 0)
                    {
                        label.Text += ",";
                    }
                    label.Text += keys[i].ToString();
                }
            }
        }

        private void KeyDownClose_KeyDown(object sender, KeyEventArgs e)
        {
            if (btnP1.Text == "停止记录")
            {
                model.P1.Add(e.KeyData);
            }
            else if (button2.Text == "停止记录")
            {
                model.P2.Add(e.KeyData);
            }
            else if (button3.Text == "停止记录")
            {
                model.P3.Add(e.KeyData);
            }
            SetLable();
        }

        private void checkBox1_Click(object sender, EventArgs e)
        {
            keyboardHook.OpenFlag1 = checkBox1.Checked;
        }

        private void checkBox2_Click(object sender, EventArgs e)
        {
            keyboardHook.OpenFlag2 = checkBox2.Checked;
        }

        private void checkBox3_Click(object sender, EventArgs e)
        {
            keyboardHook.OpenFlag3 = checkBox3.Checked;
        }

        private void checkBox4_Click(object sender, EventArgs e)
        {
            this.TopMost = checkBox4.Checked;
        }
    }
}
